% This is a sample program done by three students in the INEL 5309 class.
% The only thing that should be changed here are the titles in figures 1,
% 6 and 12.





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Digital Signal Processing (INEL 5309)             November 27,1996  %
% Computer Homework #4              		    Prof. Shawn Hunt  %
%								      %
%                          Group Members			      %
%                Luissette Ramirez -- Group Leader                    %
%                        Jose A. Rodriguez     			      %
%                         David O. Lastra			      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Program Description:						      %
%  This program creates a FIR Low Pass Filter and IIR Low Pass Filter %
%using the functions provided by MATLAB, for the following            % 
%specifications                                                       %
%    	* Linear Phase                                                %
%	* Passband ripple of 2dB                                      %
%	* Stopband rejection of 20 dB                                 %
%	* Passband of .4*pi rad/sec                                   %
%	* Stopband of .6*pi rad/sec     			      %
%     Also, the program graphs the group delay for each filter and    %
% compares the input signal with the same signal after filtering to   %
% see if the response agrees with the delay specified in the group    %
% delay graph. We observe Matlab's filter algorithm introduces some   %
% distortion at the beginning of the signal. The measure of delays are% 
% taken after the signal havs achieved steady state. In all cases this% 
% was after the tenth sample.                                         %
% Inputs: This program requires no input from the user.               %
% Outputs: This program produces 9 graphs. 		 	      %
%       Figure 1: FIR filter Frequency Response                       %
%       Figure 2: Passband Ripple               		      %	
%	Figure 3: Transition Response	   			      %	
%	Figure 4: Stopband Response                                   %
%	Figure 5: FIR Group Delay                		      %
%	Figure 6: Input Signal together with same signal filtered     % 	
%	Figure 7: IIR filter Frequency Response			      %	
%       Figure 8: Passband Ripple               		      %	
%	Figure 9: Transition Response	   			      %	
%	Figure10: Stopband Response                                   %
%	Figure11: FIR Group Delay    				      %
%	Figure12: Input Signal together with same signal filtered     %	
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
%%% Initialize the variables %%% 
n = 18;
Wn = .5;
samples = 100;
KdB = 10.5;

%%% Creates the FIR filter %%%
b = fir1(n,Wn);
K = 10^(KdB/20);
bK = K*b;
w1 = [0:pi/samples:pi];
[h,w] = freqz(bK,1,samples);

%%% Creates two input signal %%% 
t=[0:100];
x1=cos(.2*pi*t);
x2=cos(.5*pi*t);

%%% Plotting The FIR Filter Frequency Response in dB %%% 
figure(1)
clg
subplot(211),plot(w,20*log10(abs(h)))
title('FIR Filter Frequency Response (Magnitude)')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
grid
subplot(212),plot(w,(angle(h)))
title('FIR Filter Frequency Response (Phase)')
xlabel('Frequency(rad/sample)')
ylabel('Phase(rad)') 
grid
pause

%%% Create Vertical Lines to indicated the specifications %%%
vydown = [9:-1:-120];
vsize = size(vydown);
vxdown = ones(1,vsize(2));
vyup1 = [11:1:20];
vsize = size(vyup1);
vxup1 = ones(1,vsize(2));
vyup = [-9:1:20];
vsize = size(vyup);
vxup = ones(1,vsize(2));			

xstopband = [.6*pi:(.6*pi)/100:pi];
xsize = size(xstopband);  
ystopband=ones(1,xsize(2));
xpassband = [0:(.4*pi)/100:.4*pi]
xsize = size(xpassband);
ypassband=ones(1,xsize(2));		

%%% Plotting Passband Ripple %%%
figure(2)
clg
plot(w,20*log10(abs(h)))
title('Passband Ripple Response')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
line(xstopband,-9*ystopband)
line(xpassband,9*ypassband)
line(xpassband,11*ypassband)
line(.4*pi*vxdown,vydown)
line(.4*pi*vxup1,vyup1)
line(.6*pi*vxup,vyup)		
axis([0,.5*pi,8.5,11.5])
grid    
pause

%%% Plotting Transition %%%
figure(3)
clg
plot(w,20*log10(abs(h)))
title('Transition Response')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
grid

%%% Create Vertical Lines to indicated the specifications %%%
line(xstopband,-9*ystopband)
line(xpassband,9*ypassband)
line(xpassband,11*ypassband)
line(.4*pi*vxdown,vydown)
line(.4*pi*vxup1,vyup1)
line(.6*pi*vxup,vyup)		
axis([.38*pi,.62*pi,-15,10])    
pause

%%% Plotting Stopband %%%
figure(4)
clg
plot(w,20*log10(abs(h)))
title('Stopband Response')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
grid

%%% Create Vertical Lines to indicated the specifications %%%
line(xstopband,-9*ystopband)
line(xpassband,9*ypassband)
line(xpassband,11*ypassband)
line(.4*pi*vxdown,vydown)
line(.4*pi*vxup1,vyup1)
line(.6*pi*vxup,vyup)		
axis([.59*pi,.62*pi,-15,-5])    
pause

%%% Plotting the group delay of the FIR filter %%%
figure(5)
clg
[delay,f]= grpdelay(bK,1,100);
plot(f,delay)
title('FIR Group Delay')
xlabel('Rad/Sec')
ylabel('Samples') 
axis([0,pi,0,15])
grid
pause

%%% Creates the vertical lines to indicate one point of the input signal %%%
%%% and the same point in the filtered signal %%%
vydown=[-5:.25:5];
sizevy=size(vydown);
vxdown=ones(1,sizevy(2));

%%% Plotting the inputs signal together with the filtered signal %%%
figure(6)
clg
sizesign = size(x1);
tn = (0:sizesign(2)-1);
y1 = filter(bK,1,x1);
subplot(211),plot(tn,x1,tn,y1)
title('Input Signal and filtered signal #1')
xlabel('Samples')
ylabel('Amplitude')
line(10*vxdown,vydown)
line(19*vxdown,vydown)
grid
axis([0,20,-4,4])
y2 = filter(bK,1,x2);
subplot(212),plot(tn,x2,tn,y2)
title('Input Signal and filtered signal #2')
xlabel('Samples')
ylabel('Amplitude')
line(10*vxdown,vydown)
line(19*vxdown,vydown)
grid
axis([0,20,-4,4])
pause


%%% Create the IIR Filter %%%

[N,Wc] = buttord(.4,.6,2,20);
[b,a] = butter(N,Wc);
K = 10^(KdB/20);
bK = K*b;
[h,w] = freqz(bK,a,samples);

%%% Plotting The IIR Filter Frequency Response %%% 
figure(7)
clg
subplot(211),plot(w,20*log10(abs(h)))
title('IIR Filter Frequency Response (Magnitude)')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
grid
subplot(212),plot(w,(angle(h)))
title('IIR Filter Frequency Response (Phase)')
xlabel('Frequency(rad/sample)')
ylabel('Phase(rad)') 
grid
pause

%%% Create Vertical Lines to indicated the specifications %%%
vydown = [9:-1:-120];
vsize = size(vydown);
vxdown = ones(1,vsize(2));
vyup1 = [11:1:20];
vsize = size(vyup1);
vxup1 = ones(1,vsize(2));
vyup = [-9:1:20];
vsize = size(vyup);
vxup = ones(1,vsize(2));			

%%% Plotting Passband Ripple %%%
figure(8)
clg
plot(w,20*log10(abs(h)))
title('Passband Ripple Response')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
line(xstopband,-9*ystopband)
line(xpassband,9*ypassband)
line(xpassband,11*ypassband)
line(.4*pi*vxdown,vydown)
line(.4*pi*vxup1,vyup1)
line(.6*pi*vxup,vyup)		
axis([0,.5*pi,8.5,11.5])
grid    
pause


%%% Plotting Transition %%%
figure(9)
clg
plot(w,20*log10(abs(h)))
title('Transition Response')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
grid

%%% Create Vertical Lines to indicated the specifications %%%
line(xstopband,-9*ystopband)
line(xpassband,9*ypassband)
line(xpassband,11*ypassband)
line(.4*pi*vxdown,vydown)
line(.4*pi*vxup1,vyup1)
line(.6*pi*vxup,vyup)		
axis([.38*pi,.62*pi,-15,10])    
pause

%%% Plotting Stopband %%%
figure(10)
clg
plot(w,20*log10(abs(h)))
title('Stopband Response')
xlabel('Rad/Sample')
ylabel('Amplitude (dB)') 
grid

%%% Create Vertical Lines to indicated the specifications %%%
line(xstopband,-9*ystopband)
line(xpassband,9*ypassband)
line(xpassband,11*ypassband)
line(.4*pi*vxdown,vydown)
line(.4*pi*vxup1,vyup1)
line(.6*pi*vxup,vyup)		
axis([.59*pi,.62*pi,-15,-5])    
pause

%%% Plotting the group delay of the IIR filter %%%
figure(11)
clg
[delay,f]= grpdelay(bK,a,100);
plot(f,delay)
title('IIR Group Delay')
xlabel('Rad/Sec')
ylabel('Samples') 
line(.2*pi*vxdown,vydown)
line(.5*pi*vxdown,vydown)
axis([0,pi,0,6])
grid
pause



%%% Plotting the inputs signal together with the filtered signal %%%
figure(12)
clg
y1 = filter(bK,1,x1);
subplot(211),plot(tn,x1,tn,y1)
title('Input Signal and filtered signal #1')
xlabel('Samples')
ylabel('Amplitude')
grid
line(10*vxdown,vydown)
line(12.5*vxdown,vydown)
axis([0,20,-5,5])
subplot(212),plot(tn,x2,tn,y2)
title('Input Signal and filtered signal #2')
xlabel('Samples')
ylabel('Amplitude')
line(17*vxdown,vydown)
line(12*vxdown,vydown)
grid
axis([0,20,-2,2])
pause


